﻿#include "qsendfiledialog.h"
#include "ui_qsendfiledialog.h"

QSendFileDialog::QSendFileDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QSendFileDialog)
{
    ui->setupUi(this);
    {
        // 获取当前窗口标志
        Qt::WindowFlags flags = this->windowFlags();
        // 清除所有标志，然后重新设置需要的标志
        flags |= Qt::CustomizeWindowHint;          // 设置自定义窗口标志
        flags &= ~Qt::WindowCloseButtonHint;       // 清除关闭按钮标志
        flags &= ~Qt::WindowTitleHint;             // 清除标题栏标志
        flags &= ~Qt::WindowMinMaxButtonsHint;    // 清除最小化和最大化按钮标志

        // 设置修改后的标志集
        this->setWindowFlags(flags);
    }

    this->setAcceptDrops(true);

    connect(ui->close_BT,&QPushButton::clicked,[this](){
        QVBoxLayout* fileList=(QVBoxLayout*)ui->fileList_WT->layout();
        while(fileList->count()!=0){
            if(fileList->count()>1){
                int height=this->maximumHeight()-50;
                this->setMinimumHeight(height);
                this->setMaximumHeight(height);
            }
            QWidget* item=fileList->itemAt(0)->widget();
            fileList->removeWidget(item);
            item->deleteLater();
         }
        this->hide();
    });
    connect(ui->cancel_BT,&QPushButton::clicked,[this](){
        ui->close_BT->clicked();
    });
    connect(ui->send_BT,&QPushButton::clicked,[this](){
        emit sendFile(this->email,getFileList());
    });
}

QSendFileDialog::~QSendFileDialog()
{
    delete ui;
}
void QSendFileDialog::mousePressEvent(QMouseEvent* event){
    if (event->button() == Qt::LeftButton) {
        isDragging = true;
        lastPos =event->pos(); // 鼠标按下时记录鼠标相对窗体的位置
    }
}
void QSendFileDialog::mouseMoveEvent(QMouseEvent* event){
    if (isDragging&&event->buttons() & Qt::LeftButton) {
        // 移动窗口
        move(event->globalPos() - lastPos);//event->globalPos()记录窗体的原点在整个电脑窗口的位置
    }
}
void QSendFileDialog::mouseReleaseEvent(QMouseEvent* event){
    if (event->button() == Qt::LeftButton && isDragging&&!isMaximized()) {
        isDragging = false;
    }
}
void QSendFileDialog::dragEnterEvent(QDragEnterEvent *event){
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    }
}
void QSendFileDialog::dropEvent(QDropEvent *event){
    const QMimeData *mimeData = event->mimeData();
    if (mimeData->hasUrls()) {
        QList<QUrl> urls = mimeData->urls();
        for (const QUrl &url : urls) {
            // 获取文件路径（如果 URL 指向本地文件）
            QString filePath = url.toLocalFile();
            addFile(filePath);
        }
        event->acceptProposedAction();
    }
}
void QSendFileDialog::closeClicked(){
    ui->close_BT->clicked();
}
void QSendFileDialog::setCurrentObject(const QString &email,const QString &name){
    this->email=email;
    if(QFile::exists(QString("./accountData/Res/%1/headImg/head.png").arg(email))){
        ui->headImg_LB->setStyleSheet(QString("\
            #headImg_LB{\
                border-radius:25px;\
                border-image: url('./accountData/Res/%1/headImg/head.png') 0 0 0 0 stretch stretch;\
            }\
        ").arg(email));
    }
    ui->name_LB->setText(name);
}
void QSendFileDialog::addFile(const QString filePath){
    QVBoxLayout* fileList=(QVBoxLayout*)ui->fileList_WT->layout();
    FileItem* item=new FileItem(filePath);
    if(fileList->count()>0)
    {
        int height=this->maximumHeight()+70;
        this->setMinimumHeight(height);
        this->setMaximumHeight(height);
    }
    fileList->addWidget(item);
    updateItemNum(fileList->count());
    connect(item,&FileItem::fileItemDelete,this,&QSendFileDialog::deleteFileItem);
}
void QSendFileDialog::deleteFileItem(FileItem* item){
    QVBoxLayout* fileList=(QVBoxLayout*)ui->fileList_WT->layout();
    if(fileList->count()>1){
        int height=this->maximumHeight()-70;
        this->setMinimumHeight(height);
        this->setMaximumHeight(height);
        fileList->removeWidget(item);
        item->deleteLater();
        updateItemNum(fileList->count());
    }
    else
    {
        fileList->removeWidget(item);
        item->deleteLater();
        updateItemNum(fileList->count());
        this->hide();
    }
}
void QSendFileDialog::updateItemNum(const int &num){
    QRegularExpression re("\\d+");
    ui->send_BT->setText( ui->send_BT->text().replace(re,QString::number(num)));
}
QList<QString> QSendFileDialog::getFileList(){
    QList<QString> list;
    QVBoxLayout* fileList=(QVBoxLayout*)ui->fileList_WT->layout();
    for(int i=0;i<fileList->count();i++){
        FileItem* item=(FileItem*)fileList->itemAt(i)->widget();
        list.append(item->getFilePath());
    }
    return list;
}
